package com.yourenbang.lingyun.transaction.mq;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.yourenbang.lingyun.core.exception.BizRuntimeException;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.transaction.web.domain.dto.migrate.kuaishou.MigrateDTO;
import com.yourenbang.lingyun.transaction.web.service.migrate.PayTransactionMigrateService;
import com.yourenbang.lingyun.transaction.web.service.pay.klks.PayEposService;
import com.yourenbang.lingyun.transaction.web.service.pay.klks.PayP35Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 交易-拉卡拉（交易数据同步使用：蓝牙，扫码，电签，D0提款）
 */
@Slf4j
@Component
public class TradeLakalaMessageListener implements MessageListener {

    private static final String PAY_TRANSACTION = "pay_transaction";
    private static final String PAY_TRANSACTION_INDEX = "pay_transaction_index";
    private static final String PAY_WITHDRAWAL = "pay_withdrawal";

    @Autowired
    private PayTransactionMigrateService payTransactionMigrateService;
    @Autowired
    private PayEposService payEposService;
    @Autowired
    private PayP35Service payP35Service;

    @Override
    public Action consume(Message message, ConsumeContext context) {

        String msgkey = message.getKey();
        String dataStr = new String(message.getBody(), StandardCharsets.UTF_8);
        KLog.info("[MQ-交易-拉卡拉-数据同步]消息key:[{}],接收参数:{}", msgkey, dataStr);
        try {
            MigrateDTO migrateDTO = JSONObject.parseObject(dataStr, MigrateDTO.class);
            if (StringUtils.equals(migrateDTO.getTabelName(), PAY_TRANSACTION_INDEX)) {
                payTransactionMigrateService.payTransactionIndex(migrateDTO);
            } else if (StringUtils.contains(migrateDTO.getTabelName(), PAY_TRANSACTION)) {
                payTransactionMigrateService.payTransaction(migrateDTO);
            } else if (StringUtils.equals(migrateDTO.getTabelName(), PAY_WITHDRAWAL)) {
                payTransactionMigrateService.payWithdrawal(migrateDTO);
            } else {
                dealFrom(migrateDTO, msgkey);
            }
            return Action.CommitMessage;
        } catch (BizRuntimeException e) {
            KLog.error("[MQ-交易-拉卡拉-数据同步]业务异常:{},消息key:[{}],接收参数:{}", e.getErrorMessage(), msgkey, dataStr, e);
            return Action.CommitMessage;    //返回成功
        } catch (Exception e) {
            KLog.error("[MQ-交易-拉卡拉-数据同步]异常:{},消息key:[{}],接收参数:{}", e.getMessage(), msgkey, dataStr, e);
            return Action.ReconsumeLater;
        }
    }

    /**
     * 处理交易来源
     */
    private void dealFrom(MigrateDTO migrateDTO, String msgkey) {
        if (StringUtils.equals(migrateDTO.getFrom(), MigrateDTO.FROM_EPOS)) {
            payEposService.notice(migrateDTO);
        } else if (StringUtils.equals(migrateDTO.getFrom(), MigrateDTO.FROM_P35)) {
            payP35Service.notice(migrateDTO);
        } else {
            KLog.error("[MQ-交易-拉卡拉-数据同步]不支持的数据,消息key:[{}],接收参数:{}", msgkey, migrateDTO.toJSON());
        }
    }

}
